home *** CD-ROM | disk | FTP | other *** search
- Path: nuscc.nus.sg!wongsiaw
- From: wongsiaw@iscs.nus.sg (Virus)
- Newsgroups: comp.lang.c++
- Subject: [HELP!!!] Segmentation Fault!
- Date: 6 Apr 1996 01:15:22 GMT
- Organization: ...I am a wuggies...
- Message-ID: <4k4gja$mm@nuscc.nus.sg>
- NNTP-Posting-Host: wongsiaw@ibmunx.iscs.nus.sg
- X-Newsreader: TIN [version 1.2 PL2]
-
- Help !!!
- [This is going to be a very long mail, I sincerely hope that you can
- read thro it to help a novice C++ programmer.]
-
- I keep getting segmentation fault in my program, and I have tried using the
- debugger (gdb in unix), but I cannot understand what the debugger is trying
- to say..
-
- [my story]
- I have 2 files, that I am suppose to read in, one is space-separated and
- the other is comma-separated. In each of the files, there are student
- records, each terminated by a "end".
-
- This is the space-separated file, sfile.data .
-
- space-separated
-
- Undergrad "Khoo Kay Chong" 946788H13
- IC252 90 IC251 95 IC205 85 end
-
- Graduate "Lee Su Shi" 901234N13 Master TA
- IC401 80 IC501 85 end
-
- Graduate "James Bond" 880007N13 PhD RA
- IC405 85 IC504 90 IC512 95 end
-
- Exchange "Tom Hanks" 905678N13 "Stanford University"
- IC101 80 end
-
-
- And the comma-separated file looks the same except that it uses the
- comma to separate the fields of the record.
-
- I am to write 3 classes, StudentFile, SpaceStudentFile and CommaStudentFile.
- I have already done those, but I ran into segmentation fault when I
- try to run my compiled program.
-
- here are the files..
-
- If you think you need to see Student.h, Student.c String.h, String.c,
- ExchStudent.h ExchStudent.c, GradStudent.h, GradStudent.c, pls e-mail
- me and let me know.
-
- I really appreciate your help, as this is only part of my program.
- I still have other classes to write that access this class to hand in
- my final program. (but currently I am stuck here !!!)
-
- Thanx!!!
-
- regards,
- Diana
- ps : I hope that all the comments is sufficient for you to understand my
- codes.
-
- -------------------------------------------------------------------------
-
- //StudentFile.h
-
- // Need to put "virtual" in front of the object name and
- // make it = 0 if it is a deferred obj
-
- #ifndef StudentFile_h
- #define StudentFile_h
-
- #include "Student.h"
-
-
- // Constants that needed to be used in the program
- #define UNDERGRAD "Undergrad"
- #define GRADUATE "Graduate"
- #define EXCHANGE "Exchange"
- #define PHD "PhD"
- #define MASTER "Master"
- #define SCHOLARSHIP "Scholarship"
- #define TEACH "Teach"
- #define RESEARCH "Research"
- #define NONE "None"
- #define END "end"
-
- class StudentFile
- {
- public :
- // Public deferred method
- // For opening a file for reading
- // Opens the file by the name filename for reading
- // if file exists, can be opened and the opened file has the
- // correct file-type, then it returns non-0 (true)
- // Otherwise, returns 0 (false)
- virtual int open (const char *filename) = 0;
-
- // Public deferred method
- // For closing a file
- virtual void close () = 0;
-
- // Public polymorphic (non-deferred) method
- // For reading a student's record
- // Reads the next student's record, stores it in a dynamically
- // created instance, and returns a pointer to the instance
- // The dynamically created instance should belong to either
- // Student, GradStudent, ExchStudent class depending on the
- // student-type
- // Returns NULL if there is no more record in the file
- Student *readNextRecord ();
-
- protected :
- // Protected deferred method
- // For reading a field from the file
- // Reads the next field from the file and returns a pointer
- // to the field
- // This method is used to support readNextRecord
- virtual char *readNextField () = 0;
-
-
- FILE *fd;
- };
-
- #endif
-
-
- -----------------------------------------------------------------------
-
- // I think the problem is here !!!
- // StudentFile.c
-
- #include <string.h>
- #include "StudentFile.h"
- #include "GradStudent.h"
- #include "ExchStudent.h"
-
-
- // Public polymorphic (non-deferred) method
- // For reading a student's record
- // Reads the next student's record, stores it in a dynamically
- // created instance, and returns a pointer to the instance
- // The dynamically created instance should belong to either
- // Student, GradStudent, ExchStudent class depending on the
- // student-type
- // Returns NULL if there is no more record in the file
-
- Student* StudentFile::readNextRecord ()
- {
- Student *Stud;
- char *field1, *field2, *field3, *field4, *field5;
-
- // field1 - field5 are used for reading in of diff fields in a record
- // field1 read in from the record, this field now states the student-type
-
-
- // strcpy(field1, readNextField());
- field1 = (readNextField());
-
- printf("%s\n",field1); //debug stmt
-
-
- if (field1 == NULL) return NULL;
- // nothing to read in
-
- printf("%s\n",field1);//debug stmt
-
- field2 = (readNextField());
- // next field is the StudName, so get the Student name in a record
-
- printf("%s\n",field2);//debug stmt
-
- printf("%s %s \n", field1, field2);
- field3 = strdup(readNextField());
- // get the Student number in a record
-
- printf("%s\n",field3);//debug stmt
-
- // field1 has already read in the Student type, ie Graduate, Exchange, etc
- // so now need to check what is being read in
-
- printf("%s\n", field1);//debug stmt
-
- // BIG problem !!! field1 now actually has the content of
- // field3 !!! in fact all the field1, field2 and field3 has the
- // same content !!! WHY????
-
- // Check for Undergraduates
- if (!strcmp(field1, "Undergrad")) {
- printf("Create Undergrad here!\n");
- Stud = new Student(field2, field3);
- // The student type read in is Undergrade, an instance of Student class
- // will be created, and field2 = StudName, field3 = StudNo
-
- printf("Undergrad created!\n");
- }
-
- // Check for Graduates
- else if (!strcmp(field1, GRADUATE))
- {
- int Stud_lvl, Assist_type;
-
- // Now check the Gradute level
- if (!strcmp(field4 = readNextField(), PHD))
- Stud_lvl = GradStudent::PhD;
- else if (!strcmp(field4, MASTER))
- Stud_lvl = GradStudent::Master;
-
- // Now check the assistant-ship, make field4 read the next field
- if (!strcmp(field4 = readNextField(), TEACH))
- Assist_type = GradStudent::TA;
- else if (!strcmp(field4, SCHOLARSHIP))
- Assist_type = GradStudent::Scholarship;
- else if (!strcmp(field4, RESEARCH))
- Assist_type = GradStudent::RA;
- else if (!strcmp(field4, NONE))
- Assist_type = GradStudent::None;
-
- // An instance of GradStudent class will be created
- GradStudent *Grad = new GradStudent(field2, field3, Stud_lvl, Assist_type);
- // field2 = StudName, field3 = StudNo,
- Stud = Grad;
- }
-
- // Check for Exchange Students
- else if (!strcmp(field1, EXCHANGE))
- {
- // An instance of ExchStudent class will be created
- ExchStudent *Exch = new ExchStudent(field2, field3, readNextField());
- // field2 = StudName, field3 = StudNo, readNextField() = parentU
- Stud = Exch;
- }
-
- // Now read course names and grades
- while (!strcmp(field5 = readNextField(), END))
- {
- int Course_Gr;
-
- // read the grade of the course
- sscanf(readNextField(), "%i", &Course_Gr);
- // field5 = course_name
- Stud -> assignGrade(field5, Course_Gr);
- }
-
- return Stud;
- } // end of readNextRecord
-
- -------------------------------------------------------------------------
-
- // SpaceStudentFile.h
-
- #ifndef SpaceStudentFile_h
- #define SpaceStudentFile_h
-
- #include "StudentFile.h"
-
- class SpaceStudentFile : public StudentFile
- {
- public :
- // Open the cfile.data for reading..
- int open (const char *filename);
-
- // Close the file after reading
- void close ();
-
- private :
-
- // Read the next field from the file - record
- char *readNextField();
-
- };
-
- #endif
-
-
- // SpaceStudentFile.c
-
- #include "SpaceStudentFile.h"
-
- // Fields in sfile.data are seperated by one or more spaces
-
- int SpaceStudentFile::open (const char *filename)
- {
- char buffer[20];
-
- fd = fopen (filename, "r");
- if (fd == NULL) {
- printf("Cannot open file : %s \n", filename);
- return 0;
- }
- fscanf(fd, "%s", buffer);
- return(!strcmp(buffer, "space-separated"));
- }
-
-
- void SpaceStudentFile::close ()
- {
- fclose(fd);
- }
-
- char* SpaceStudentFile::readNextField()
- {
- char str[40];
- int i = 0;
-
- // take away starting whitespaces
- while (str[0] = fgetc(fd)) {
- if (*str == ' ') continue;
- if (*str == '\t') continue;
- if (*str == '\n') continue;
- if (*str == EOF) return NULL;
- break;
- }
-
- // read from " to next "
- if (str[0] == '"') {
- for (i=0; ((str[i] = fgetc(fd)) != '"'); i++)
- ;
- str[i]=0;
- return str;
- }
-
- // else read until next whitespace
- for (i=1; ((str[i] = fgetc(fd)) != ' ')
- && (str[i] != '\n') && (str[i] != '\t'); i++)
- ;
- str[i] = 0;
- return str;
-
-
- }
-
- ----------------------------------------------------------------------
-
- // CommaStudentFile.h
-
- #ifndef CommaStudentFile_h
- #define CommaStudentFile_h
-
- #include "StudentFile.h"
-
- class CommaStudentFile : public StudentFile
- {
- public :
- // Open the cfile.data for reading..
- int open (const char *filename);
-
- // Close the file after reading
- void close ();
-
- private :
-
- // Read the next field from the file - record
- char *readNextField();
-
- };
-
- #endif
-
- ----------------------------------------------------------------------
-
- // CommaStudentFile.c
-
- #include "CommaStudentFile.h"
-
- // Fields in cfile.data are seperated by whitespaces and commas
-
- int CommaStudentFile::open (const char *filename)
- {
- char buffer[20];
-
- fd = fopen(filename, "r");
- if (fd == NULL) {
- printf("Cannot open file :%s\n", filename);
- return 0;
- }
- fscanf(fd, "%s", buffer);
- return (!strcmp(buffer, "comma-separated"));
-
- }
-
-
- void CommaStudentFile::close()
- {
- fclose(fd);
- }
-
- char* CommaStudentFile::readNextField()
- {
- char str[40];
- int i=0;
-
- // take away whitespace, for more comments, see SpaceStudentFile.c
- while (str[0] = fgetc(fd)) {
- if (*str == ' ') continue;
- if (*str == '\n') continue;
- if (*str == '\t') continue;
- if (*str == EOF) return "EOF";
- break;
- }
-
- // read until next comma
- for (i=0; ((str[i]=fgetc(fd)) != ',') && (str[i] != '\n'); i++)
- { printf("%d",i); }
- ;
- printf("\n");
- str[i] =0;
-
- // take away trailing whitespace
- for (; (str[i-1] == ' '); i--);
- str[i+1] = 0;
-
- return str;
-
-
- }
-
- ------------------------------------------------------------------------
-
-
- --
- -== .DV. ==- -== Windows Users Group (Singapore) ==-
- http://www.iscs.nus.sg/~wongsiaw http://www.ncb.gov.sg/WUG
- --
-